package trabajoISW2.presentation.server;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import trabajoISW2.data.AddressDAO;
import trabajoISW2.data.CreditCardDAO;
import trabajoISW2.data.IAddressDAO;
import trabajoISW2.data.ICreditCardDAO;
import trabajoISW2.data.IOrderDAO;
import trabajoISW2.data.IUserDAO;
import trabajoISW2.data.OrderDAO;
import trabajoISW2.data.UserDAO;

import trabajoISW2.domain.implementations.Address;
import trabajoISW2.domain.implementations.CreditCard;
import trabajoISW2.domain.implementations.Order;
import trabajoISW2.domain.implementations.User;
import trabajoISW2.presentation.client.OrderManagementUseCase;
import trabajoISW2.presentation.shared.AddressDTO;
import trabajoISW2.presentation.shared.AddressMapper;
import trabajoISW2.presentation.shared.CreditCardDTO;
import trabajoISW2.presentation.shared.CreditCardMapper;
import trabajoISW2.presentation.shared.OrderDTO;
import trabajoISW2.presentation.shared.OrderMapper;
import trabajoISW2.presentation.shared.UserDTO;

public class OrderManagement implements
		OrderManagementUseCase {

	private ICreditCardDAO cDao = new CreditCardDAO();
	private IOrderDAO oDao = new OrderDAO();
	private IOrderDAO oDao2 = new OrderDAO();
	private IOrderDAO oDao3 = new OrderDAO();
	private IAddressDAO aDao = new AddressDAO();
	private IUserDAO uDao = new UserDAO();

	@Override
	public OrderDTO selectCreditCardAndAddress(String creditCard, int addressId, int orderId) {
		OrderDTO dto = null;
		Order o = oDao.getOrder(orderId);
		Address ad = aDao.getAddress(addressId);
		CreditCard cc = cDao.getCreditCard(creditCard);
		if (o == null)
			System.err.println("There is no order with id " + orderId);
		else if (cc == null)
			System.err.println("There is no credit card with number "
					+ creditCard);
		else {
			o.setCreditCard(cc);
			o.setAddress(ad);
			dto = OrderMapper.createDTO(o);
			oDao.updateOrder(o);
			
		}

		return dto;

	}



	@Override
	public OrderDTO confirmOrder(int orderId) {
		OrderDTO dto = null;
		Order o = oDao2.getOrder(orderId);
		if (o == null)
			System.err.println("There is no order with id " + orderId);
		else if (o.getAddress() == null)
			System.err.println("You must select an address");
		else if (o.getCreditCard() == null)
			System.err.println("You must select an address");
		else {
			o.setFinished(true);
			dto = OrderMapper.createDTO(o);
			oDao3.updateOrder(o);
	
		}

		return dto;

	}

	@Override
	public void showInvoice(int orderId) {
		Order o = oDao2.getOrder(orderId);
		if (o == null)
			System.err.println("Order " + orderId + " does not exist");
		else if (!o.isFinished())
			System.err
					.println("Unable to show invoice until order is finalized");
		else
			o.showInvoice();

	}

	@Override
	public List<CreditCardDTO> getCards(UserDTO user) {
		CreditCard cc;
		List<CreditCardDTO> dtos = new LinkedList<CreditCardDTO>();
		User u = uDao.getUser(user.getUserId());
		if (u == null)
			System.err.println("User \"" + user.getUserId()
					+ "\" doesn't exist");
		else {

			Iterator<CreditCard> it = u.getCards().iterator();
			while (it.hasNext()) {
				cc = it.next();
				dtos.add(CreditCardMapper.createDTO(cc));
			}
		}
		return dtos;
	}

	@Override
	public List<AddressDTO> getAddresses(UserDTO user) {
		Address ad;
		List<AddressDTO> dtos = new LinkedList<AddressDTO>();
		User u = uDao.getUser(user.getUserId());
		if (u == null)
			System.err.println("User \"" + user.getUserId()
					+ "\" doesn't exist");
		else {
			Iterator<Address> it = u.getAddresses().iterator();
			while (it.hasNext()) {
				ad = it.next();
				dtos.add(AddressMapper.createDTO(ad));
			}
		}
		return dtos;
	}

}
